package com.sc.page;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sc.bean.GoodsBean;
import com.sc.data.GoodsData;

public class DelScart extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// fix
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String path = req.getContextPath();
		resp.setContentType("text/html");

		//通过id
		String id = req.getParameter("id");

		//从柜子中取出购物车
		HttpSession s = req.getSession();
		List<GoodsBean> sc = (List<GoodsBean>) s.getAttribute("sc");
		if (sc == null) {
			sc = new ArrayList<GoodsBean>();
		}

		Iterator<GoodsBean> it = sc.iterator();
		while(it.hasNext()){
			GoodsBean g = it.next();
			if(g.getId().toString().equals(id)){
				it.remove();
				break;
			}
		}
		
		//购物车放回柜子
		s.setAttribute("sc", sc);

		// resp
		resp.sendRedirect(path + "/scart.html");

	}

}
